/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.propertytree.propertyhandler;

import java.io.Serializable;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import hu.tetete.hibiscus.R;
import hu.tetete.hibiscus.model.OneSelectProperty;
import hu.tetete.hibiscus.propertytree.PropertyTreeService;
import hu.tetete.hibiscus.propertyvalue.SelectPropertyValue;

/**
 * TODO javadoc.
 */
public class OneSelectListHandler implements PropertyHandler<OneSelectProperty, SelectPropertyValue> {

    /** @see Serializable */
    private static final long serialVersionUID = 1L;

    /**
     * The context.
     */
    private final Context                  context;

    /**
     * Service to handle the property tree values.
     */
    private final PropertyTreeService      propertyTreeService;

    /**
     * The property.
     */
    private final OneSelectProperty        property;

    /**
     * @param context The context
     * @param propertyTreeService TODO javadoc.
     * @param property The property
     */
    public OneSelectListHandler(final Context context,
            final PropertyTreeService propertyTreeService,
            final OneSelectProperty property) {
        this.context             = context;
        this.propertyTreeService = propertyTreeService;
        this.property            = property;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getView()
     */
    @Override
    public View getView() {

        final LayoutInflater        inflater      = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View                  view          = inflater.inflate(R.layout.multiselect_linear, null);
        final ListView              listView      = (ListView)view.findViewById(R.id.optionList);
//        final ListView              listView      = (ListView)inflater.inflate(R.layout.multiselect_property, null);

        final SelectPropertyValue[] options       = new SelectPropertyValue[this.property.getOptions().size() + 1];
        final SelectPropertyValue   selectedValue = getValue();

        int index = 0;
        int selectedPosition = 0;

        options[index++] = new SelectPropertyValue(null, "Not selected");

        for (final SelectPropertyValue option : this.property.getOptions()) {
            if (selectedValue == option) {
                selectedPosition = index;
            }
            options[index++] = option;
        }

        final ArrayAdapter<SelectPropertyValue> adapter = new ArrayAdapter<SelectPropertyValue>(this.context, android.R.layout.simple_list_item_single_choice, options);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setItemChecked(selectedPosition, true);
//        listView.setSelection(selectedPosition);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(final AdapterView<?> arg0, final View arg1, final int position,
                    final long id) {
                @SuppressWarnings("hiding")
                final SelectPropertyValue selectedValue = options[position];
                if (selectedValue.getId() == null) {
                    setValue(null);
                } else {
                    setValue(selectedValue);
                }
//                Toast.makeText(MultiSelectHandler.this.context, "You clicked: " + position + " id: " + id, Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getInfoView()
     */
    @Override
    public View getInfoView() {
        final TextView textView = new TextView(this.context);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        final SelectPropertyValue value = getValue();
        final String text;
        if (value == null) {
            text = "Not selected";
        } else {
            text = value.getName();
        }
        textView.setText(text);
        textView.setTextSize(8);
        textView.setMaxLines(3);
        textView.setMaxWidth(30);
        return textView;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getProperty()
     */
    @Override
    public OneSelectProperty getProperty() {
        return this.property;
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#setValue(hu.tetete.hibiscus.propertyvalue.PropertyValue)
     */
    @Override
    public void setValue(final SelectPropertyValue value) {
        if (value == null) { //TODO or isEmpty()
            this.propertyTreeService.removeValue(this.property.getId());
        }
        this.propertyTreeService.putValue(this.property.getId(), value);
    }

    /**
     * @see hu.tetete.hibiscus.propertytree.propertyhandler.PropertyHandler#getValue()
     */
    @Override
    public SelectPropertyValue getValue() {
        return (SelectPropertyValue)this.propertyTreeService.getValue(this.property.getId());
    }

}
